package com.wanlian.service.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.common.Verification;
import com.wanlian.dao.CardDao;
import com.wanlian.dao.ProductCovertHistoryDao;
import com.wanlian.dao.ProductDao;
import com.wanlian.dao.UserDao;
import com.wanlian.entity.Card;
import com.wanlian.entity.Product;
import com.wanlian.entity.User;
import com.wanlian.error.ErrorCode;
import com.wanlian.model.ResultModel;
import com.wanlian.service.CardServices;
import com.wanlian.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CardServicesImpl implements CardServices {

    @Autowired
    public CardDao cardDao;

    @Autowired
    public ProductDao productDao;

    @Autowired
    public UserDao userDao;

    @Autowired
    public ProductCovertHistoryDao productCovertHistoryDao;


    @Override
    public ResultModel doAdd(Card card) {
        ResultModel resultModel = new ResultModel();

        //验证卡号卡密
        Boolean code_yz=Verification.isCode(card.getCode());
        Boolean pass_yz=Verification.isPass(card.getPassword());
        if(!code_yz||!pass_yz){
            resultModel.setMessage("请验证19位数字卡号与18位数字卡密");
            resultModel.setBresult(false);
            return resultModel;
        }

        Card dbCard = cardDao.queryByCode(card.getCode());
        if (dbCard != null) {
            resultModel.setBresult(false);
            resultModel.setMessage(ErrorCode.CARD_CODE_HASEXIST);
        } else {
            Product product = productDao.queryById(card.getProductId());
            if (product != null) {
                card.setName(product.getName());
                card.setScore(product.getScore());
                card.setImgPath(product.getImgPath());
                card.setRmb(product.getRmb());
                User dbUser = userDao.queryById(card.getAddedUserId());
                if (dbUser != null) {
                    resultModel.setBresult(false);
                    resultModel.setMessage(ErrorCode.NO_USER_ID);
                } else {
                        cardDao.add(card);
                        Product productdb = productDao.queryById(card.getProductId());
                        productdb.setStock(product.getStock() + 1);
                        productDao.update(productdb);
                        resultModel.setMessage("添加成功");
                        resultModel.setBresult(true);
                }
            } else {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CARD_PRODUCT_NO_EXIST);
            }
        }
        return resultModel;
    }

    /**
     * 批量导入卡号
     * @param cards
     * @return
     */
    @Override
    public ResultModel doAdd(List<Card> cards,int productId) {
        ResultModel resultModel = new ResultModel();
        for (Card card : cards) {
            card.setProductId(productId);
            Card dbCard = cardDao.queryByCode(card.getCode());
            if (dbCard != null) {
                resultModel.setBresult(false);
                resultModel.setMessage(ErrorCode.CARD_CODE_HASEXIST);
            } else {
                Product product = productDao.queryById(card.getProductId());
                if (product != null) {
                    card.setName(product.getName());
                    card.setScore(product.getScore());
                    card.setImgPath(product.getImgPath());
                    card.setRmb(product.getRmb());
                    User dbUser = userDao.queryById(card.getAddedUserId());
                    if (dbUser != null) {
                        resultModel.setBresult(false);
                        resultModel.setMessage(ErrorCode.NO_USER_ID);
                    } else {
                        cardDao.add(card);
                        Product dbProduct = productDao.queryById(card.getProductId());
                        dbProduct.setStock(product.getStock() + 1);
                        productDao.update(dbProduct);
                        resultModel.setMessage("添加成功");
                        resultModel.setBresult(true);
                    }
                } else {
                    resultModel.setBresult(false);
                    resultModel.setMessage(ErrorCode.CARD_PRODUCT_NO_EXIST);
                }
            }
        }
        return resultModel;
    }

    @Override
    public ResultModel doedit(Card card) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 通过页码和条件查询
     *
     * @param pageCondition
     * @return
     */
    @Override
    public ResultModel queryCard(PageCondition pageCondition) {
        return cardDao.queryCard(pageCondition);
    }

    @Override
    public ResultModel queryCardByUser(Page page, int userId) {
        return cardDao.queryCardByUser(page, userId);
    }

    /**
     * 卡片兑换
     *
     * @param card
     * @param user
     * @return
     */
    @Override
    public ResultModel doConverCardByUser(Card card, User user) {
        Card dbCard = this.cardDao.queryById(card.getId());
        if (dbCard != null) {
            if (dbCard.isEnable()) {
                User dbUser = this.userDao.queryById(user.getId());
                if (dbUser != null) {
                    if (!dbCard.isHasUsed()) {
                        dbCard.setUserId(user.getId());
                        dbCard.setHasUsed(true);
                        this.cardDao.update(dbCard);
                        return new ResultModel(true);
                    } else {
                        return new ResultModel(false, ErrorCode.CARD_HAS_USEED);
                    }
                } else {
                    return new ResultModel(false, ErrorCode.NO_USER_ID);
                }
            } else {
                return new ResultModel(false, ErrorCode.CARD_NO_ENABLE);
            }
        } else {
            return new ResultModel(false, ErrorCode.CARD_NO_ID);
        }
    }


}
